package com.saibo.spider.gather.service;

import com.alibaba.fastjson.JSONArray;
import com.saibo.spider.main.MainControl;
import com.saibo.spider.utils.EncodeUtil;
import com.saibo.spider.utils.HttpRequestUtil;
import com.saibo.spider.utils.HttpResponseUtil;
import com.saibo.spider.utils.StringUtil;
import org.apache.http.client.methods.CloseableHttpResponse;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MissionService {
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getMissionList() {
		//String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/taskList";
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/taskList";
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		String json = HttpResponseUtil.getHtml(httpResponse);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> jsonList = (List<Map<String, Object>>) JSONArray.parseArray(json, map.getClass());
		return jsonList;
	}

	public String getMissionListByTaskId(String id) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/taskDetail?taskID=" + id;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		String json = HttpResponseUtil.getHtml(httpResponse);
		return json;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> getPageActionListByTaskId(String id) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/infoClassList?taskID=" + id;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		String json = HttpResponseUtil.getHtml(httpResponse);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> jsonList = (List<Map<String, Object>>) JSONArray.parseArray(json, map.getClass());
		return jsonList;
	}

	public static void main(String[] args) {
		MissionService s = new MissionService();
		s.getMissionList();
	}

	public void insertTaskMission() {
	}

	/*
	 * Source
	 */
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> getSourceList() {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/sourceList";
		Map<String,String> mso = new HashMap<String,String>();
		mso.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		if(null==httpResponse)
			return null;
		String json = HttpResponseUtil.getHtml(httpResponse);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> jsonList = (List<Map<String, Object>>) JSONArray.parseArray(json, map.getClass());
		return jsonList;
	}

	public static String updateSource(String text) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/updateSource?jsonStr="
				+ EncodeUtil.encodeURL(text, "UTF-8");
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static String insertSource(String text) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/insertSource?jsonStr="
				+ EncodeUtil.encodeURL(text, "UTF-8");
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static String deleteSource(String text) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/deleteSource?sourceID=" + text;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	/**
	 * Task
	 * 
	 * @param text
	 * @return
	 */

	public static String deleteTask(String text) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/deleteTask?taskId=" + text;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static String updateTask(String text) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/updateTask?jsonStr="
				+ text;
		//EncodeUtil.encodeURL(text, "UTF-8");
		System.err.println(url);
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static String deleteTaskEntry(String entryId) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/deleteTaskEntry?entryID=" + entryId;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static Map<String, Object> getTaskEntry(String entryId) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/getTaskEntryDetail?taskEntryId=" + entryId;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		String json = HttpResponseUtil.getHtml(httpResponse);
		Map<String, Object> map = StringUtil.jsonToMap(json);
		return map;
	}

	public static String updateEntryTask(String text) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/updateTaskEntry?jsonStr="
				+ EncodeUtil.encodeURL(text, "UTF-8");
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static String insertTaskEntry(String text) {
		String url = MainControl.dataServiceUrl+"/heiheihei/spiderweb/task/insertTaskEntry?jsonStr="
				+ EncodeUtil.encodeURL(text, "UTF-8");
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	// Schema
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> getSchemaList() {
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/schemaList";
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		String json = HttpResponseUtil.getHtml(httpResponse);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> jsonList = (List<Map<String, Object>>) JSONArray.parseArray(json, map.getClass());
		return jsonList;
	}

	public static String getSchemaDetail(String id) {
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/updateSchema?jsonStr=" + id;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static String updateSchema(String text) {
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/updateSchema?jsonStr="
				+ EncodeUtil.encodeURL(text, "UTF-8");
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static String insertSchema(String text) {
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/insertSchema?jsonStr="
				+ EncodeUtil.encodeURL(text, "UTF-8");
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	public static String deleteSchema(String entryId) {
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/deleteSchema?schemaID=" + entryId;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}

	/**
	 * TableList
	 */

	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> getTableList(String t) {
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/table/getTableListWithSchema?schemaID=" + t;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		String json = HttpResponseUtil.getHtml(httpResponse);
		Map<String, Object> map = new HashMap<String, Object>();
		List<Map<String, Object>> jsonList = (List<Map<String, Object>>) JSONArray.parseArray(json, map.getClass());
		return jsonList;
	}
	
	
	
	/**
	 * Classinfo信息
	 */
	
	public static String getInfoClassDetail(String id) {
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/getInfoClassDetail?infoClassId=" + id;
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}
	
	public static String insertClassInfoMap(String text){
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/insertInfoClass?jsonStr=" + EncodeUtil.encodeURL(text, "UTF-8");
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}
	
	public static String updateClassInfoMap(String text){
		String url = "http://192.168.1.210:8080/heiheihei/spiderweb/task/updateInfoClass?jsonStr=" + EncodeUtil.encodeURL(text, "UTF-8");
		CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, null);
		return HttpResponseUtil.getHtml(httpResponse);
	}
}
